alter procedure p_chktnamelike
(
  @tname nvarchar(60),
  @tid integer,
  @likename nvarchar(4000) output
)
as
begin
  DECLARE @position int, @r nvarchar(4000), @chk nvarchar(10)
    , @chkstr nvarchar(4000), @tmpname nvarchar(60), @chkname nvarchar(60)
    , @existsname nvarchar(60)
  set @r=''
  select @chk=p_paramvalue from ts_parameters where P_ParamNM='tradernamelikecheck'
  if isnull(@chk,'')<>'1'
  begin
    set @likename=''
    return
  end
  
  select @chkstr=p_paramvalue from ts_parameters where P_ParamNM='tradernamelikestr'
  set @position = charindex(';',@chkstr)
  WHILE (@position > 0) or (@chkstr<>'')
  BEGIN
    if @position=1
    begin
      set @chkstr=substring(@chkstr,2,len(@chkstr))
    end
    else
    begin
      if @position=0
      begin
        set @tmpname=@chkstr
        set @chkstr=''
      end    
      else
      begin
        set @tmpname=substring(@chkstr, 1, @position-1)
        set @chkstr=substring(@chkstr,@position+1,len(@chkstr))      
      end
      select @chkname=REPLACE(@tname,@tmpname,'')
      --print @chkstr+'  chk:'+@chkname+' tmp:'+@tmpname
      
      set @existsname=''
      declare chkname_cur cursor local forward_only static read_only for
      select name from td_trader where client=1 and traderid<>@tid and REPLACE(name,@tmpname,'') like '%'+@chkname+'%'
      open chkname_cur
      fetch next from chkname_cur into @existsname
      while @@fetch_status=0
      begin
        if ISNULL(@existsname, '') <> ''
        begin
          if @r=''  
            set @r=@r+','+@existsname+','
          else if charindex(','+@existsname+',',@r)<=0
          begin
            set @r=@r+@existsname+','        
          end
        end
        fetch next from chkname_cur into @existsname
      end
      close chkname_cur
      deallocate chkname_cur        
    end
    set @position = charindex(';',@chkstr)
    --print @chkstr+'  chk:'+@chkname+' tmp:'+@tmpname    
  END
  set @likename=@r
end